<template>
  <el-dialog :close-on-click-modal="false"  v-model="isShow">
    <template #title>
      <div class="card-header">
        <i
            class="el-icon-edit-outline title-icon"
        ></i>
        <span class="title-font">{{title}}</span>
      </div>
      <el-divider></el-divider>
    </template>
    <el-form label-position="right" :rules="rules" ref="dataForm" label-width="80px" :model="dataForm">
      <el-form-item label="权限名称" prop="authName">
        <el-input :readonly="isView" v-model="dataForm.authName" placeholder="权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限标识"  prop="authCode">
        <el-input :readonly="isView" v-model="dataForm.authCode" placeholder="权限标识" ></el-input>
      </el-form-item>
      <el-form-item label="排序字段"  >
        <el-input-number :readonly="isView" v-model="dataForm.sortIndex" placeholder="排序字段" :min="0" :max="100" ></el-input-number>
      </el-form-item>
      <el-form-item label="菜单名称"  >
        <el-input :readonly="true" v-model="dataForm.menuName"  ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button v-loading="loading" @click="close">取 消</el-button>
        <el-button v-loading="loading" type="primary" v-if="!isView" @click="save">保 存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import VueUtil from "@/util/VueUtil";
import {NuoAuthController} from "@/actions/controller";

export default {
  name: "auth-add-update-form",
  emits:["reload"],
  data(){
    return{
      isShow: false,
      title: '',
      isView: true,
      loading: false,
      dataForm: {
        authName: '',
        authCode: '',
        menuName: '',
        menuId: '',
        sortIndex: 0
      },
      rules:{
        authName: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 1, max: 64, message: '权限名称长度为1 - 64个字符', trigger: 'blur' }
        ],
        authCode: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { min: 1, max: 64, message: '权限标识长度为1 - 64个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    init(title,data,isView = false){
      this.$nextTick(()=>{
        if(this.$refs.dataForm) this.$refs.dataForm.clearValidate()
      })
       this.title = title
       if(data){
           Object.assign(this.dataForm,data)
       }
       this.isView = isView
       this.isShow = true
    },
    save(){
      VueUtil(this).formVerify("dataForm",()=>{
        this.loading = true
        VueUtil(this).ajax(NuoAuthController.saveAuth,this.dataForm).then(res=>{
          VueUtil(this).hint(res.message)
          this.loading = false
          this.$emit("reload")
          this.close()
        }).catch(()=>{
          this.loading = false
        })
      })
    },
    close(){
      Object.assign(this.$data, this.$options.data());
    }
  }
}
</script>

<style scoped>

</style>